package com.crishne.anwo.utill;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;


@Component("file_download_view")
public class FileDownloadView implements View {

	
	public String getContentType() {
		return "application/octet-stream; charset=UTF-8";
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void render(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(this.getContentType());
		String stFilename = (String)map.get("stFilename");
		byte[] byContent = (byte[])map.get("byContent");
	    response.setHeader("Content-disposition","attachment; filename=\"" + URLEncoder.encode(stFilename, "UTF-8") + "\"");
	    OutputStream os = response.getOutputStream();
	    os.write(byContent);
	    os.flush();
	    os.close();
	}

}
